package com.neiru.pruebas.ar;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.neiru.pruebas.ar.figure.Cube;
import com.neiru.pruebas.ar.renderer.HelloOpenGLES20Renderer;
import com.neiru.pruebas.ar.thread.LaunchCubeThread;
import com.neiru.pruebas.ar.view.CustomCameraView;

public class PruebasARActivity extends Activity {
	
	private CustomCameraView cv;
	
	private GLSurfaceView mGLView;
	
	private HelloOpenGLES20Renderer mRenderer = null;
	
	private List<Cube> figures;
	
	private final SensorEventListener mListener = new SensorEventListener() {
		
		public void onSensorChanged(SensorEvent event) {
			if(mRenderer != null) {
				
				// con event.values[2] obtengo el angulo de vision, si miro arriba o abajo
				// desde -90 hasta 90
				
				float northRotation = 450.0f - event.values[0];
				float radRotation = northRotation * 2*(((Double)Math.PI).floatValue())  / 360;
				mRenderer.rotates[2] = ((Double)Math.cos(radRotation)).floatValue();
				mRenderer.rotates[0] = ((Double)Math.sin(radRotation)).floatValue();
			}
			
		}
		
		
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			
		}
	};
	
	private final SensorEventListener mRotationListener = new SensorEventListener() {
		
		public void onSensorChanged(SensorEvent event) {
			// event.values[2] -1 abajo, 0 arriba 
			Log.d("PruebasARActivity", "Event [" + event.values[0] + ", " + event.values[1] + ", " + event.values[2] + "]");
		}
		
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			
		}
	};
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        SensorManager mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        Sensor mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        Sensor mSensor2 = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
        mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_GAME);

        mSensorManager.registerListener(mRotationListener, mSensor2, SensorManager.SENSOR_DELAY_GAME);
        
        
        
        cv = new CustomCameraView(this.getApplicationContext());
        FrameLayout r1 = new FrameLayout(this.getApplicationContext());
        setContentView(r1);
        mGLView = new GLSurfaceView(this);
//        mGLView.setEGLContextClientVersion(2);
        mGLView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);

        figures = new ArrayList<Cube>();
        for (int i = 0; i < 20; i++) {
        	String color = "";
        	if(i < 7) {
        		color = "red";
        	} else if (i < 14) {
        		color = "green";
        	} else if (i < 21) {
        		color = "blue";
        	}
        	Cube cube = new Cube(1, 1, 1, color);
        	float x = ((Double)(Math.random()*((Math.random() <= 0.5)?-1:1))).floatValue()*20;
        	float y = 0;
        	float z = ((Double)(Math.random()*((Math.random() <= 0.5)?-1:1))).floatValue()*20;
        	cube.x = x;
        	cube.y = y;
        	cube.z = z;
        	figures.add(cube);
        	//CubeThread cubeThread = new CubeThread(cube);
        	//cubeThread.start();
        }
        
        
        
        mRenderer = new HelloOpenGLES20Renderer(figures);
        mGLView.setRenderer(mRenderer);
//        mGLView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        mGLView.setZOrderOnTop(true);
        
        r1.addView(mGLView);
        r1.addView(cv);
        
        mGLView.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				if(MotionEvent.ACTION_DOWN == event.getAction()) {
					synchronized (figures) {
						Cube cube = new Cube(.3f, .3f, .3f, "green");
						figures.add(cube);
						Thread thread = new LaunchCubeThread(cube, mRenderer.rotates, figures);
						thread.start();
					}
				}
				return true;
			}
		});
    }

    @Override
    protected void onPause() {
        super.onPause();
        // The following call pauses the rendering thread.
        // If your OpenGL application is memory intensive,
        // you should consider de-allocating objects that
        // consume significant memory here.
        mGLView.onPause();
        
        this.finish();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // The following call resumes a paused rendering thread.
        // If you de-allocated graphic objects for onPause()
        // this is a good place to re-allocate them.
        mGLView.onResume();
    }
    
}